package com.eks.websocket.v2;

import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@RequiredArgsConstructor
@RequestMapping("/websocket/server")
public class TestController {

    private final WebSocketServer webSocketServer;

    @PostMapping("/send")
    public String sender(@RequestParam String key, @RequestParam String message) throws IOException {
        if(StringUtils.isBlank(key)){
            webSocketServer.sendMessage(message);
        }else{
            webSocketServer.sendMessage(message, key);
        }
        return "OK";
    }
}
